package cn.kgc.ebook.service.impl;

import cn.kgc.ebook.dao.EbookcategoryMapper;
import cn.kgc.ebook.dao.EbookentryMapper;
import cn.kgc.ebook.pojo.Ebookcategory;
import cn.kgc.ebook.pojo.Ebookentry;
import cn.kgc.ebook.pojo.EbookentryExample;
import cn.kgc.ebook.service.EbookKentryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookKentryServiceImpl implements EbookKentryService {

    @Resource
    EbookentryMapper ebookentryMapper;

    @Override
    public PageInfo<Ebookentry> select(Integer pageNum, Integer pageSize, String categoryId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Ebookentry> ebookentries;
        if(!"0".equals(categoryId)){
            EbookentryExample example=new EbookentryExample();
            EbookentryExample.Criteria criteria = example.createCriteria();
            criteria.andCatrgoryidEqualTo(categoryId);
             ebookentries = ebookentryMapper.selectByExample(example);
        }else{
            ebookentries = ebookentryMapper.selectByExample(null);
        }

        PageInfo<Ebookentry> pageInfo=new PageInfo<>(ebookentries);
        return pageInfo;
    }

    @Override
    public Integer add(Ebookentry entry) {
        return ebookentryMapper.insert(entry);
    }

    @Override
    public Ebookentry seleById(Integer id) {
        return ebookentryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(Ebookentry entry) {
        return ebookentryMapper.updateByPrimaryKeySelective(entry);
    }

    @Override
    public Integer delete(Integer id) {
        return ebookentryMapper.deleteByPrimaryKey(id);
    }
}
